﻿function chooseImage(file){
	//Choose and show the new image for board
	debug.trace(file);
	img_choose.src=file;
}

/**
 * Drag image here!
 */  
function dragImage() {
  var images = Utils.createDragFilesImagesList(event.dragFiles);
  var numImages = images.length;
  if (numImages>0){
		img_choose.src = images[0];
		debug.trace(images[0]);
  }
}

/**
 * Utils namespace
 */
var Utils = {};

/**
 * Convert event.dragfiles object to an array of strings
 * @param {Object} obj collection of drag files
 * @return {Array} Array of filepaths
 */
Utils.createDragFilesImagesList = function(obj) {
  var files = [];
  if (!obj) {
    return files;
  }
  var e = new Enumerator(obj);
  var validExtensions = {
    png: true,
    gif: true,
    jpg: true,
    jpeg: true }; 
  while (!e.atEnd()) {
    var path = e.item();
    var extension = Utils.extractExtension(path).toLowerCase();
    if (validExtensions[extension] === true) {
      files.push(path + '');
    }
    e.moveNext();
  }
  return files;
};

/**
 * Extract the extension from a path
 * @param {String} The path
 * @return {String} The extension
 */
Utils.extractExtension = function(s) {
  return s.substring(s.lastIndexOf('.') + 1); 
};